package com.middlegroup.pay.web.api;

import com.alibaba.fastjson.JSONObject;
import com.middlegroup.common.constants.MethodConstants;
import com.middlegroup.common.model.NotifyRequest;
import com.middlegroup.common.model.ResponseEntity;
import com.middlegroup.pay.entity.InBills;
import com.middlegroup.pay.entity.InNotifyLog;
import com.middlegroup.pay.entity.OutBills;
import com.middlegroup.pay.entity.OutNotifyLog;
import com.middlegroup.pay.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;


/**
 * 回调接收
 * @author pc
 * @date 2019/1/15
 */
@RestController
@RequestMapping("notify")
@Slf4j
public class NotifyController extends BaseController {

    @Resource
    private InBillsService inBillsService;

    @Resource
    private InNotifyLogService inNotifyLogService;

    @Resource
    private OutBillsService outBillsService;

    @Resource
    private OutNotifyLogService outNotifyLogService;

    @Resource
    private SubjectPayService subjectPayService;
    @Resource
    private WechatPayH5Service wechatPayH5Service;
    @Resource
    private PaypalService paypalService;
    @Resource
    private AlipayService alipayService;
    /**代付回调
     * @param request
     * @return
     */
    @RequestMapping(value = "df")
    ResponseEntity df(@Valid NotifyRequest request){
        //TODO 记录主体服务端回调通知数据
        OutNotifyLog outNotifyLog = new OutNotifyLog();
        outNotifyLog.setBillNo(request.getBillNo());
        outNotifyLog.setMsg(request.getMsg());
        outNotifyLog.setTransNo(request.getTransNo());
        outNotifyLog.setMerchantBillNo(request.getMerchantBillNo());
        //默认处理中
        outNotifyLog.setStatus(0);
        outNotifyLog.setCallbackText(JSONObject.toJSONString(request));
        boolean rt = outNotifyLogService.insert(outNotifyLog);
        if (!rt){
            return new ResponseEntity(ResponseEntity.STATUS_FAIL,null,"插入回调日志表失败!",null);
        }
        //TODO 接收回调，修改中台业务数据
        OutBills outBills = outBillsService.getByBillNo(request.getBillNo());
        if (null != outBills && outBills.getStatus().intValue() == 0){
            outBills.setStatus(request.getTransStatus());
            outBills.setMsg(request.getMsg());
            rt = outBillsService.updateById(outBills);
            //如果更新成功则通知商户
            if (rt){
                //通知商户
                subjectPayService.notifyMerchant(outBills.getMerchantCode(), MethodConstants.DF_NOTIFY,request);
            }
            //回调处理成功置为1
            outNotifyLog.setStatus(1);
            outNotifyLogService.updateById(outNotifyLog);
        }

        //TODO 转发回调给商户，记录日志
        return new ResponseEntity(ResponseEntity.STATUS_OK,null,"接收成功",null);
    }

    /**代收回调
     * @param request
     * @return
     */
    @RequestMapping(value = "deduct")
    ResponseEntity deduct(@Valid NotifyRequest request){
        //TODO 记录主体回调数据
        InNotifyLog inNotifyLog = new InNotifyLog();
        inNotifyLog.setBillNo(request.getBillNo());
        inNotifyLog.setMerchantBillNo(request.getMerchantBillNo());
        inNotifyLog.setStatus(0);
        inNotifyLog.setTransNo(request.getTransNo());
        inNotifyLog.setCallbackText(JSONObject.toJSONString(request));
        boolean rt = inNotifyLogService.insert(inNotifyLog);
        if (!rt){
            return new ResponseEntity(ResponseEntity.STATUS_FAIL,null,"插入回调日志表失败!",null);
        }
        //TODO 接收回调，修改中台业务数据
        InBills inBills = inBillsService.getByBillNo(request.getBillNo());
        if (null != inBills){
            inBills.setStatus(request.getTransStatus());
            inBills.setMsg(request.getMsg());
            rt = inBillsService.updateById(inBills);
            //如果更新成功则通知商户
            if (rt){
                //TODO 通知商户
                subjectPayService.notifyMerchant(inBills.getMerchantCode(), MethodConstants.DEDUCT_NOTIFY,request);
            }
            inNotifyLog.setStatus(1);
            inNotifyLogService.updateById(inNotifyLog);
        }
        //TODO 转发回调给商户，记录日志
        return new ResponseEntity(ResponseEntity.STATUS_OK,null,"接收成功",null);
    }

    /**协议支付回调
     * @param request
     * @return
     */
    @RequestMapping(value = "protocolPay")
    ResponseEntity protocolPay(@Valid NotifyRequest request){
        //TODO 记录主体回调数据
        InNotifyLog inNotifyLog = new InNotifyLog();
        inNotifyLog.setBillNo(request.getBillNo());
        inNotifyLog.setMerchantBillNo(request.getMerchantBillNo());
        inNotifyLog.setStatus(0);
        inNotifyLog.setTransNo(request.getTransNo());
        inNotifyLog.setCallbackText(JSONObject.toJSONString(request));
        boolean rt = inNotifyLogService.insert(inNotifyLog);
        if (!rt){
            return new ResponseEntity(ResponseEntity.STATUS_FAIL,null,"插入回调日志表失败!",null);
        }
        //TODO 接收回调，修改中台业务数据
        InBills inBills = inBillsService.getByBillNo(request.getBillNo());
        if (null != inBills){
            inBills.setStatus(request.getTransStatus());
            inBills.setMsg(request.getMsg());
            rt = inBillsService.updateById(inBills);
            //如果更新成功则通知商户
            if (rt){
                //TODO 通知商户
                subjectPayService.notifyMerchant(inBills.getMerchantCode(), MethodConstants.DEDUCT_NOTIFY,request);
            }
            inNotifyLog.setStatus(1);
            inNotifyLogService.updateById(inNotifyLog);
        }

        //TODO 转发回调给商户，记录日志
        return new ResponseEntity(ResponseEntity.STATUS_OK,null,"接收成功",null);
    }

    /**快捷支付回调
     * @param request
     * @return
     */
    @RequestMapping(value = "quickPay")
    ResponseEntity quickPay(@Valid NotifyRequest request){
        //TODO 记录主体回调数据
        InNotifyLog inNotifyLog = new InNotifyLog();
        inNotifyLog.setBillNo(request.getBillNo());
        inNotifyLog.setMerchantBillNo(request.getMerchantBillNo());
        inNotifyLog.setStatus(0);
        inNotifyLog.setTransNo(request.getTransNo());
        inNotifyLog.setCallbackText(JSONObject.toJSONString(request));
        boolean rt = inNotifyLogService.insert(inNotifyLog);
        if (!rt){
            return new ResponseEntity(ResponseEntity.STATUS_FAIL,null,"插入回调日志表失败!",null);
        }
        //TODO 接收回调，修改中台业务数据
        InBills inBills = inBillsService.getByBillNo(request.getBillNo());
        if (null != inBills){
            inBills.setStatus(request.getTransStatus());
            inBills.setMsg(request.getMsg());
            rt = inBillsService.updateById(inBills);
            //如果更新成功则通知商户
            if (rt){
                //TODO 通知商户
                subjectPayService.notifyMerchant(inBills.getMerchantCode(), MethodConstants.DEDUCT_NOTIFY,request);
            }
            inNotifyLog.setStatus(1);
            inNotifyLogService.updateById(inNotifyLog);
        }

        //TODO 转发回调给商户，记录日志
        return new ResponseEntity(ResponseEntity.STATUS_OK,null,"接收成功",null);
    }
    @RequestMapping(value = "/payPalNotify")
    public void payPalNotify(HttpServletRequest request) {
        log.info("paypal, 进入异步通知接口...");
        paypalService.payPalNotify(request);
    }

    @RequestMapping(value = "/alipayNotifyNotice")
    public void alipayNotifyNotice(HttpServletRequest request, HttpServletRequest response) throws Exception {
        log.info("支付成功, 进入异步通知接口...");
        alipayService.aliPayH5Notify(request);
    }

    @RequestMapping(value = "/wechatPayH5Notify")
    public String wechatPayH5Notify(HttpServletRequest request) throws Exception {
        log.info("wechatPayH5Notify, 进入异步通知接口...");
        return wechatPayH5Service.wechatPayH5Notify(request);
    }

    @RequestMapping(value = "/wechatH5RefundNotify")
    public String wechatH5RefundNotify(HttpServletRequest request) throws Exception {
        log.info("wechatH5RefundNotify, 进入异步通知接口...");
        return wechatPayH5Service.wechatH5RefundNotify(request);
    }

}
